var NEWS_MODELNAME = 'com.dartfar.news.model.News';
var NEWS_MODELIDNAME = 'id';
var NEWS_BUSINESSNAME = 'News';
var NEWS_DATAGRID;
var NEWS_AUTVIEW;
var NEWS_CU_WINDOW;	//编辑窗口
var NEWS_Q_WINDIW;
var NEWS_WFBTN;	//工作流按钮
var NEWS_BSBTN;	//业务按钮
var NEWS_ROW;


$(function() {
	//初始化增删改查面板
	CU_PANEL_INIT(GETID(NEWS_BUSINESSNAME + 'WindowPanel'), NEWS_BUSINESSNAME);
	//初始化查询面板
	QU_PANEL_INIT(GETID(NEWS_BUSINESSNAME + 'QueryPanel'), NEWS_BUSINESSNAME);
	
	NEWS_BSBTN = GETID(NEWS_BUSINESSNAME + 'Btn');
	//初始化业务按钮
	PERM_BTN_INIT(NEWS_BSBTN, NEWS_BUSINESSNAME);
		
	//初始化查询界面
	initQueryUI();
	
	//var autCode = $('#lstDeparts').combobox('getValue');
	
	//初始化查询窗口
	NEWS_Q_WINDIW = GETID(NEWS_BUSINESSNAME + 'QueryWindow');
	BS_WINDOW_INIT(NEWS_Q_WINDIW, 500, 400);
	
	
	//初始化编辑窗口
	NEWS_CU_WINDOW = GETID(NEWS_BUSINESSNAME + 'Window');
	BS_WINDOW_INIT(NEWS_CU_WINDOW, 800, 600);
	

	
	
	NEWS_DATAGRID = GETID(NEWS_BUSINESSNAME + 'DataGrid');
	var URL = 'newsAjax!loadNewsData';
	var COLUMNS = [
	               {field:'id',title:'编号',width:50},
	               {field:'person',title:'作者',width:150},
	               {field:'depart', title:'部门',width:150},
	               {field:'title',title:'标题',width:500},
	               {field:'createDate',title:'日期',width:150}
	              ];
	var TOOLBAR = '#' + NEWS_BUSINESSNAME + 'ToolBar';
	
	DG_INIT(NEWS_DATAGRID, URL, COLUMNS, true, true, TOOLBAR, NEWS_BUSINESSNAME);

	//初始化文本编辑器
	$('#newsContent').xheditor({width:'100%',height:'300',tools:'Blocktag,Fontface,FontSize,|,Bold,Italic,Underline,|,FontColor,BackColor,|,Align,List,|,Link,Img,Hr,Table',skin:'o2007blue'});
});


//保存方法
function SAVEFUNCTION(){
	//赋值
	var newsId = $('#newsId').val();
	var newsTitle = $('#newsTitle').val();
	var newsContent = $('#newsContent').val();
	if(newsId == ''){
		$.post('newsAjax!add', {title:newsTitle, content:newsContent},
				function(data){
					DG_RELOAD(NEWS_DATAGRID);
					MESSAGEBOX('新闻增加成功.');
					NEWS_CU_WINDOW_CLOSE();
			}, 'json');
	}else{
		$.post('newsAjax!edit', {id:newsId, title:newsTitle, content:newsContent},
				function(data){
					DG_RELOAD(NEWS_DATAGRID);
					MESSAGEBOX('新闻编辑成功.');
					NEWS_CU_WINDOW_CLOSE();
			}, 'json');
	}

}

//关闭CRUD窗口
function NEWS_CU_WINDOW_CLOSE(){
	WINDOW_CLOSE(NEWS_CU_WINDOW);
}

//初始化查询模块界面
function initQueryUI(){
	NEWS_AUTVIEW = GETID(NEWS_BUSINESSNAME + '_AUT_VIEW');
	QUERY_INIT(NEWS_AUTVIEW, 200);

}

//打开窗口方法
function OPEN_News_WINDOW(type){
	//清空
	$('#newsId').val('');
	$('#newsTitle').val('');
	$('#newsContent').val('');
	
	SET_CU_FUNCTION(SAVEFUNCTION, NEWS_CU_WINDOW_CLOSE);
	
	//增加
	if(type == 'add'){
		BS_WINDOW_OPEN(NEWS_CU_WINDOW, '增加', 'add');
	//浏览
	}else if(type == 'view'){
		alert('view');
	//编辑
	}else if(type == 'edit'){
		NEWS_ROW = DG_GETROW(NEWS_DATAGRID);
		if(ROW_IS_NOT_NULL(NEWS_ROW)){
			AUT_EDIT(NEWS_ROW, openNewsEditWindow);
		}
	//删除
	}else if(type == 'del'){
		NEWS_ROW = DG_GETROW(NEWS_DATAGRID);
		if(ROW_IS_NOT_NULL(NEWS_ROW)){
			AUT_REMOVE(NEWS_ROW, removeNews);
		}
	//查询
	}else if(type == 'search'){
		openQueryWindow();
	//其他
	}else{
		MESSAGEBOX('非法操作!');
		return;
	}
}

//删除新闻
function removeNews(){
	$.post('newsAjax!remove', {id:NEWS_ROW.id},
			function(data){
				DG_RELOAD(NEWS_DATAGRID);
				MESSAGEBOX('数据删除成功.');
		}, 'json').error(function() {
			MESSAGEBOX('数据删除失败.');
	});
}

//打开编辑窗口
function openNewsEditWindow(){
	$('#newsId').val(NEWS_ROW.id);
	$('#newsTitle').val(NEWS_ROW.title);
	$('#newsContent').val(NEWS_ROW.content);
	BS_WINDOW_OPEN(NEWS_CU_WINDOW, '编辑', 'edit');
}

//打开查询窗口
function openQueryWindow(){
	SET_QU_FUNCTION(submitQueryNews, closeQueryWindow);
	BS_WINDOW_OPEN(NEWS_Q_WINDIW, '查询', 'search');
}

//关闭查询窗口
function closeQueryWindow(){
	WINDOW_CLOSE(NEWS_Q_WINDIW);
}

//提交查询按钮
function submitQueryNews(){
	autCode = GETAUTVIEW(NEWS_AUTVIEW);
	var URL = 'newsAjax!loadNewsData';
	var QUERY = {autCode: autCode};
	DG_QUERY_PAGE(NEWS_DATAGRID, URL, QUERY);
	
/*	NEWS_DATAGRID.datagrid('options').url = '?autCode='+ autCode + '&pageNo=1&pageSize=15';
	NEWS_DATAGRID.datagrid("options").pageNumber = 1;   
	NEWS_DATAGRID.datagrid('reload'); 
	NEWS_DATAGRID.datagrid('getPager').pagination({pageNumber: 1}); */
	closeQueryWindow();
}
